/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-03-15 15:19:50
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-03-16 13:58:24
 */
'use static'
const {
    User
} = require('../models/user')
const bcrypt = require('bcrypt');
const user = {
    async registry(username,password){
         let result = await User.findOne({
             where: {
                 username
             }
         })
         if (!result) {
            const user = await User.create({
                username,
                password
            })
            console.log(user.toJSON());
            return user;
         }
        //  console.log(result)
         return {
            dataValues:null,
            id: result.dataValues.id
         }


    },
    /**
     * @Description: 登录
     * @params: { String } userName
     * @params: { String } password
     * @return: { Object | null }
     */
    async login(userName, password) {
       let result = await User.findOne({
            where: {
                username:userName,
                //password: bcryptPassword
            }
        })
        //bcrypt.compare(用户提交的密码,数据库密码)
         const bcryptPassword = await bcrypt.compare(password, result.password);
         console.log(bcryptPassword)
         if (bcryptPassword) {
          return  result
         }
         return Error()
    },
    /**
     * @Description: 获取用户信息
     * @params: { String } 用户ID
     * @return: { Object | null }
     */
   async getUserInfo(userId) {

        let result = await User.findByPk(userId)
        return result
    },
    /**
     * @Description: 获取用户列表
     * @params: { String } 用户ID
     * @return: { Array | null }
     */
    async getUserList() {
        console.log(User.findByPk)
        let result = await User.findAll({
            attributes: ['id', 'username']
        })
        return result
    }
}
module.exports = user